<?php

require_once(LIB_DIR . "smarty/Smarty.class.php");

class C extends Smarty {

    //常用变量
    public $T = array();
    //模板文件
    private $tplFile = "";
    //模板文件后缀
    private $suffix = '.html';
    //样式文件&JS脚本文件加载
    private $cssStr, $jsStr;

    public function C() {
        $this->tplEngine();
    }

    /**
     * 自动变量设置
     * @access public
     * @param $name 属性名称
     * @param $value  属性值
     */
    public function __set($name, $value) {
        if (property_exists($this, $name))
            $this->$name = $value;
    }

    /**
     * 自动变量获取
     * @access public
     * @param $name 属性名称
     * @return mixed
     */
    public function __get($name) {
        return isset($this->$name) ? $this->$name : null;
    }

    //编译需要传递的参数
    public static function P($str=null) {
        if (is_null($str) || empty($str))
            return '';

        if (is_string($str))
            $query = base64_encode($str);

        if (is_array($str))
            $query = base64_encode(str_replace("&amp;", '&', http_build_query($str)));

        /*
         * if here is modified ,please modify other two pleases
         * 1) System/lib/class/Controller.class.php line 51
         * 2) System/lib/smarty/plugins/function.url.php 25
         */
        if (substr_count($query, '=') >= 1)
            return str_replace(array("==", '='), array(".html", ".htm"), $query);
        elseif (!empty($query))
            return $query . ".shtml";
        else
            return $query;
    }

    public static function URL($parameter=null) {
        if (is_null($parameter) || empty($parameter))
            return '';

        if (in_array('mod_rewrite', apache_get_modules()))
            $url = '/';
        else
            $url = Q;
        //$parameter 形式 module=default&class=default&function=index&id=2&uid=3 ...
        //普通的GET方式
        if (is_string($parameter)) {
            @parse_str($parameter, $output);
            unset($parameter);
            if (!empty($output['module'])) {
                $url.=$output['module'] . '/';
                unset($output['module']);
            }
            if (!empty($output['class'])) {
                $url.=$output['class'] . '/';
                unset($output['class']);
            }
            if (!empty($output['function'])) {
                $url.=$output['function'] . '/';
                unset($output['function']);
            }
            if (!empty($output))
                $url.=self::P($output);
            unset($output);
            return '/' . $url;
        }

        //$parameter 形式 array("default","default","index",array("id"=>2,"uid"=>3));
        // array("模块名","类名","方法名",array("参数名"=>"值","参数名"=>"值", ... ));
        if (is_array($parameter)) {
            $output = $parameter;
            unset($parameter);
            if (!empty($output['module'])) {
                $url.=$output['module'] . '/';
                unset($output['module']);
            }
            if (!empty($output['class'])) {
                $url.=$output['class'] . '/';
                unset($output['class']);
            }
            if (!empty($output['function'])) {
                $url.=$output['function'] . '/';
                unset($output['function']);
            }

            if (!empty($output))
                $url.=self::P($output);
            unset($output);
            return $url;
//            if (!empty($output[0])) {
//                $url.=$output[0] . '/';
//                unset($output[0]);
//            }
//            if (!empty($output[1])) {
//                $url.=$output[1] . '/';
//                unset($output[1]);
//            }
//            if (!empty($output[2])) {
//                $url.=$output[2] . '/';
//                unset($output[2]);
//            }
//            return '/' . $url . self::P($output[3]);
        }
        return '/';
    }

    public function display($resource_name='', $cache_id = null, $compile_id = null) {
        //parent::display($resource_name, $cache_id, $compile_id);
        $pathInfo = Controller::path_info();

        if (empty($resource_name))
            $resource_name = isset($pathInfo[2]) ? $pathInfo[2] : 'index';

        foreach ($tmpDir = isArray($this->template_dir) as $template_dir) {
            if (strstr($resource_name, '.') != $this->suffix)
                $resource_name = $resource_name . $this->suffix;
            $pathInfo[1] = strtolower($pathInfo[1]);
            if (file_exists($template_dir . "{$pathInfo[1]}/$resource_name"))
                $resource_name = "{$pathInfo[1]}/$resource_name";
            elseif (file_exists($template_dir . "$resource_name"))
                $resource_name = $resource_name;
            else
                throw_exception("TEMPLATE FILE NOT FOUND");
        }

        $this->tplFile = $resource_name;

        $this->prepare_for_display();

        /*
          if (file_exists($this->template_dir . $uri))
          $resource = $uri;
          elseif (file_exists($this->template_dir . $uri . ".html"))
          $resource = $uri . ".html";
          elseif (file_exists($this->template_dir . $uri . ".htm"))
          $resource = $uri . ".htm";
          elseif (file_exists($this->template_dir . $uri . ".php"))
          $resource = $uri . ".php";
          elseif (file_exists($this->template_dir . $pathInfo[1] . ".html"))
          $resource = $pathInfo[1] . ".html";
          elseif (file_exists($this->template_dir . $pathInfo[1] . ".htm"))
          $resource = $pathInfo[1] . ".htm";
          elseif (file_exists($this->template_dir . $pathInfo[1] . ".php"))
          $resource = $pathInfo[1] . ".php";
         */
        parent::display($resource_name, $cache_id, $compile_id);
    }

    private function prepare_for_display() {
        //加载CSS
        $this->textCss("public.css");
        $this->assign("css", $this->cssStr);

        //加载JS
        $this->textJs("jquery.min.js");
        $this->assign("script", $this->jsStr);

        //标签body里的Class
        $this->assign("bodyClass", isset($pathInfo[2]) ? '-' . $pathInfo[2] : '-undefined');

        //常用变量 get post
        $this->var_get_post();
    }

    function textCss($fileName) {
        $this->cssStr = '<link rel="stylesheet" type="text/css" media="all" href="' . C("SITE_URL") . 'Public/css/' . $fileName . '" />' . $this->cssStr;
    }

    function textJs($fileName) {
        $this->jsStr = '<script type="text/javascript" src="' . C("SITE_URL") . 'Public/js/' . $fileName . '"></script>' . $this->jsStr;
    }

    function var_get_post() {
        if (!empty($_GET))
            $this->T = array("get" => $_GET);
        if (!empty($_POST))
            $this->T = array("post" => $_POST);
        if (!empty($this->T))
            $this->assign("T", $this->T);
    }

    function tplEngine() {
        //smarty engine
        $this->tplSmarty();
        $this->addPluginsDir(APP_PATH . "plugins/");
    }

    function tplSmarty() {
        //parent::smarty();
        parent::__construct();
        $this->setTemplateDir(APP_PATH . 'views/');
        $this->setCompileDir(APP_PATH . 'cache/');
        $this->setCacheDir(APP_PATH . 'cache/');
        $this->setConfigDir(APP_PATH . 'config/');
        $this->left_delimiter = "<#";
        $this->right_delimiter = "#>";
    }

}
